/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.stream.IntStream;

/**
 * <p>Project: javaseapp - IntStreamDemo
 * <p>Powered by webrx On 2021-08-20 17:21:18
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class IntStreamDemo {
    public static int num = 0;


    public static void main(String[] args) {
        //建立IntStream 空流
        var is = IntStream.empty();
        System.out.println(is.count());

        //计算1+2+3+...+100=?
        //方法一
        int sum = 0;
        for (int i = 1; i <= 100; i++) sum += i;
        System.out.println("1+2+3+...+100=" + sum);

        //方法二 递归
        int sum2 = sum(100);
        System.out.println("1+2+3+...+100=" + sum2);

        //java8 IntStream
        int sum3 = IntStream.rangeClosed(1, 100).sum();
        System.out.println("1+2+3+...+100=" + sum3);
    }

    public static int sum(int i) {
        num += i--;
        if (i == 0) {
            return num;
        } else {
            return sum(i);
        }
    }
}
